package com.qf.openapi.web.master.filter;



import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@WebFilter("/*")
@Slf4j
public class MyFilter implements Filter {
    public MyFilter() {
        super();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        StringBuffer url = ((HttpServletRequest) servletRequest).getRequestURL();
        Map<String, String[]> map = servletRequest.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = map.entrySet();
        ArrayList<HashMap<String,List<String>>> lists = new ArrayList<>();
        for (Map.Entry<String, String[]> entry : entries) {
            String key = entry.getKey();
            String[] value = entry.getValue();
            List<String> list = Arrays.asList(value);
            HashMap<String,List<String>> maps = new HashMap<>();
            maps.put(key, list);
            lists.add(maps);
        }
        log.debug("请求参数为={]",lists);
        Enumeration<String> names = ((HttpServletRequest) servletRequest).getHeaderNames();
        HashMap<String,String> headers = new HashMap<>();
        while (names.hasMoreElements()) {
            String s = names.nextElement();
            String header = ((HttpServletRequest) servletRequest).getHeader(s);
            headers.put(s, header);
        }
        log.debug("请求头为={}",headers);
        long start = System.currentTimeMillis();
        log.debug("===>>url={}",url);
        filterChain.doFilter(servletRequest,servletResponse);
        long end = System.currentTimeMillis();
        log.debug("耗时={}",start-end);

    }

    @Override
    public void destroy() {

    }
}
